package net.haiyue.api.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.haiyue.api.custom.JsonResult;
import net.haiyue.api.entity.Module;
import net.haiyue.api.entity.Project;
import net.haiyue.api.entity.User;
import net.haiyue.api.entity.viewModel.ModuleViewModel;
import net.haiyue.api.service.ModuleService;
import net.haiyue.api.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(value = "ModuleController")
@RestController
@RequestMapping(value = "/module")
public class ModuleController {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private ModuleService _service;
    @Resource
    private UserService _userService;

    @ApiOperation(value="获取所有的模块信息", notes="用于表格展示")
    @GetMapping("/index")
    public JsonResult Index(@RequestParam("id") int id){
        return JsonResult.success( _service.findByProjectId(id));
    }
    @ApiOperation(value="获取模块下拉框数据")
    @GetMapping("/select")
    public  JsonResult Select(@RequestParam("id") int id){
        return JsonResult.success(_service.selectModule(id));
    }

    @PostMapping("/add")
    public JsonResult add(@RequestBody ModuleViewModel module){
        List<User> usrs= _userService.findAllById(module.getUids());
        Project project=new Project();
        project.setId(module.getPid());
        Module info=new Module();
        info.setProject(project);
        info.setName(module.getName());
        info.setPercent(module.getPercent());
        info.setTime_begin(module.getTime_begin());
        info.setPre_duration(module.getPre_duration());
        info.setComment(module.getComment());
        info.setUsers(usrs);
        return JsonResult.success(_service.add(info));
    }
}



